//
//  EntertainmentViewController.m
//  littleLight's Project-A
//
//  Created by dllo on 16/5/19.
//  Copyright © 2016年 light. All rights reserved.
//

#import "EntertainmentViewController.h"
#import "EntertainmentModel.h"
#import "EntertainmentTableViewCell.h"
#import <AFNetworking.h>
@interface EntertainmentViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *enterTableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation EntertainmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    self.enterTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,20, self.view.frame.size.width , self.view.frame.size.height - 100 ) style:UITableViewStylePlain];
    self.enterTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.enterTableView];
    self.enterTableView.delegate = self;
    self.enterTableView.dataSource = self;
    [self.enterTableView registerClass:[EntertainmentTableViewCell class] forCellReuseIdentifier:@"reuse"];
    self.enterTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
   
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://api.breadtrip.com/place/pois/nearby/?keyword=&category=21&start=0&count=20&latitude=38.883448&longitude=121.544680&sign=f79b397231c58a9e01418382cc50f434" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *array = [responseObject objectForKey:@"items"];
        for (NSDictionary *dic in array) {
            EntertainmentModel *model = [[EntertainmentModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        [self.enterTableView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
    
    
    // Do any additional setup after loading the view.
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EntertainmentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    EntertainmentModel *model = [self.dataArray objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor colorWithRed:251 / 255.0  green:248 / 255.0 blue:241 / 255.0 alpha:1];
    cell.model = model;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
